package com.wolfeyes.framework.service;

import com.wolfeyes.system.service.SysMenuService;
import com.wolfeyes.system.service.SysRoleService;
import com.wolfeyes.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @title 用户权限处理服务层组件
 * @author yanyljava
 * @create 2020-09-05 13:03
 */
@Component
public class SysPermissionService {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * @title 获取角色数据权限
	 * @param sysUser 用户信息
	 * @return 角色权限信息
	 */
	public Set<String> getRolePermission(SysUser sysUser) {
		Set<String> roles = new HashSet<>();
		// 管理员拥有所有权限
		if (sysUser.isAdmin()) {
			roles.add("admin");
		} else {
			roles.addAll(sysRoleService.selectRolePermissionByUserId(sysUser.getUserId()));
		}
		return roles;
	}

	/**
	 * @title 获取菜单数据权限
	 * @param sysUser 用户信息
	 * @return 菜单权限信息
	 */
	public Set<String> getMenuPermission(SysUser sysUser) {
		
		Set<String> perms = new HashSet<String>();
		// 管理员拥有所有权限
		if (sysUser.isAdmin())
		{
			perms.add("*:*:*");
		}
		else
		{
			perms.addAll(sysMenuService.selectMenuPermsByUserId(sysUser.getUserId()));
		}
		return perms;
	}

}


